<template>
  <el-dialog :title="type === 'add' ? '新增菜单' : '编辑菜单'"
    :visible="dialogVisible"
    width="650px"
    @close="close">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="24">
          <el-form-item label="父级菜单" prop="pid">
            <el-input v-model="ruleForm.id" v-show="false"></el-input>
            <el-input v-model="ruleForm.pid" v-show="false"></el-input>
            <el-input v-model="ruleForm.pname" readonly v-popover:popover></el-input>
            <el-popover
              ref="popover"
              placement="bottom"
              width="510"
              trigger="focus"
              v-model="flag">
              <el-tree
                :data="list"
                show-checkbox
                node-key="id"
                :props="defaultProps"
                check-on-click-node
                ref="tree"
                @check-change="checkChange"
                check-strictly>
              </el-tree>
            </el-popover>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜单名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="组件名称" prop="component">
            <el-input v-model="ruleForm.component"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="路由地址" prop="path">
            <el-input v-model="ruleForm.path"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示排序" prop="order">
            <el-input-number v-model="ruleForm.order" controls-position="right"  :min="1" :max="100"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="菜单图标" prop="icon">
            <el-input v-model="ruleForm.icon" readonly v-popover:popoverIcon clearable></el-input>
            <el-popover
              ref="popoverIcon"
              placement="bottom"
              width="510"
              trigger="focus"
              v-model="iconFlag">
              <d-icon :formIcon.sync="ruleForm.icon" :iconFlag.sync="iconFlag" />
            </el-popover>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜单类型" prop="type">
            <el-radio-group v-model="ruleForm.type">
              <el-radio label="目录"></el-radio>
              <el-radio label="菜单"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜单状态" prop="status">
            <el-radio-group v-model="ruleForm.status">
              <el-radio :label="true">启用</el-radio>
              <el-radio :label="false">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示状态" prop="visible">
            <el-radio-group v-model="ruleForm.visible">
              <el-radio :label="true">可见</el-radio>
              <el-radio :label="false">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否外链" prop="is_frame">
            <el-radio-group v-model="ruleForm.is_frame">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="ruleForm.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="reset('ruleForm')">取 消</el-button>
      <el-button v-if="type === 'add'" type="primary" @click="save('ruleForm')" :loading="loading">确 定</el-button>
      <el-button v-if="type === 'edit'" type="primary" @click="update('ruleForm')" :loading="loading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      ruleForm: {
        id: '',
        pid: '',
        pname: '',
        name: '',
        icon: '',
        component: '',
        path: '',
        order: '',
        status: true,
        visible: true,
        type: '目录',
        remark: '',
        is_frame: false
      },
      rules: {
        name: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' }
        ],
        order: [
          { required: true, message: '请输入菜单顺序', trigger: 'blur' }
        ],
        path: [
          { required: true, message: '请输入路由地址', trigger: 'blur' }
        ],
        component: [
          { required: true, message: '请输入组件地址', trigger: 'blur' }
        ],
      },
      loading: false,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      type: 'add',
      flag: false,
      iconFlag: false,
    }
  },
  props: {
    dialogVisible: {
      type: Boolean
    },
    list: {
      type: Array
    }
  },
  methods: {
    close () {
      this.$refs.ruleForm.resetFields();
      this.ruleForm.pname = ''
      this.$emit('update:dialogVisible', false)
    },
    open () {
      this.type = 'add'
      this.ruleForm.pname = ''
      this.$emit('update:dialogVisible', true)
    },
    checkChange (obj, checked) { // 节点选中状态发生变化时的回调
      if (checked) {
        this.$nextTick(() => {
          this.$refs.tree.setCheckedKeys([obj.id])
          this.flag = false
          // this.ruleForm.pid = JSON.parse(JSON.stringify(obj))
          this.ruleForm.pid = obj.id
          this.ruleForm.pname = obj.name
        })
      }
    },
    save(formName) { // 新增
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            component: this.ruleForm.component,
            icon: this.ruleForm.icon,
            is_frame: this.ruleForm.is_frame,
            name: this.ruleForm.name,
            order: this.ruleForm.order,
            remark: this.ruleForm.remark,
            status: this.ruleForm.status,
            type: this.ruleForm.type,
            path: this.ruleForm.path,
            visible: this.ruleForm.visible
          }
          if (this.ruleForm.pid) {
            params.pid = this.ruleForm.pid
          }
          this.$API({
            name: 'addMenu',
            data: params,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.msg)
              this.close()
              this.$emit('getAll')
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    reset(formName) {
      this.$refs[formName].resetFields();
      this.ruleForm.pname = ''
      this.$emit('update:dialogVisible', false)
    },
    detaliInfo (id) { // 根据id获取菜单详情
      this.type = 'edit'
      this.$emit('update:dialogVisible', true)
      this.$API({
        name: 'detailMenu',
        paths: [id],
        requireAuth: true
      }).then (res => {
        if (res.data.code === 200) {
          this.ruleForm = {
            id: res.data.data.menu.id,
            component: res.data.data.menu.component,
            icon: res.data.data.menu.icon,
            is_frame: res.data.data.menu.is_frame,
            name: res.data.data.menu.name,
            order: res.data.data.menu.order,
            remark: res.data.data.menu.remark,
            status: res.data.data.menu.status,
            type: res.data.data.menu.type,
            path: res.data.data.menu.path,
            visible: res.data.data.menu.visible,
            pid: res.data.data.menu.pid,
            pname: res.data.data.father.name
          }
          this.$refs.tree.setCheckedKeys([this.ruleForm.pid])
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(error => {
        this.$message.error(error)
      })
    },
    update(formName) { // 编辑
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            id: this.ruleForm.id,
            component: this.ruleForm.component,
            icon: this.ruleForm.icon,
            is_frame: this.ruleForm.is_frame,
            name: this.ruleForm.name,
            order: this.ruleForm.order,
            remark: this.ruleForm.remark,
            status: this.ruleForm.status,
            type: this.ruleForm.type,
            path: this.ruleForm.path,
            visible: this.ruleForm.visible,
            pid: this.ruleForm.pid
          }
          this.$API({
            name: 'updateMenu',
            data: params,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200 && res.data.data[0] === 1) {
              this.$message.success(res.data.msg)
              this.close()
              this.$emit('getAll')
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
  }
}
</script>

<style>

</style>